package zijie;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

/**
 * 给定一个 没有重复 数字的序列，返回其所有可能的全排列。
 */
public class Permute_46_22 {

    List<List<Integer>> res = new LinkedList<>();

    public List<List<Integer>> permute(int[] nums) {
        LinkedList<Integer> track = new LinkedList<>();
        dfs(nums,track);
        return res;
    }

    public void dfs(int[] nums, LinkedList<Integer> track){
        if(track.size() == nums.length){
            res.add(new LinkedList<>(track));
            return;
        }

        for (int num : nums) {
            if (track.contains(num)) {
                continue;
            }
            track.add(num);
            dfs(nums, track);
            track.removeLast();
        }
    }

    public List<List<Integer>> permute2(int[] nums) {
        LinkedList<Integer> track = new LinkedList<>();
        dfs2(nums,track);
        return res;
    }

    public void dfs2(int[] nums, LinkedList<Integer> track){
        if(track.size() == nums.length){
            res.add(new LinkedList<>(track));
            return;
        }
        for (int num : nums) {
            if(track.contains(num)){
                continue;
            }
            track.add(num);
            dfs2(nums,track);
            track.removeLast();
        }
    }


    public static void main(String[] args) {
        int[] test = {1,2,3};
        Permute_46_22 permute_46_22 = new Permute_46_22();
        permute_46_22.permute(test);
    }
}
